package com.oplink.webems.modules.alarm.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.oplink.webems.common.persistence.Page;
import com.oplink.webems.common.utils.DateUtils;
import com.oplink.webems.modules.alarm.entity.AlarmHistory;
import com.oplink.webems.modules.alarm.entity.AlarmSeverity;
import com.oplink.webems.modules.alarm.entity.AlarmType;
import com.oplink.webems.modules.alarm.service.AlarmHistoryService;
import com.oplink.webems.modules.alarm.service.AlarmPropService;
import com.oplink.webems.modules.cfg.entity.CfgNe;
import com.oplink.webems.modules.cfg.entity.CfgNeType;
import com.oplink.webems.modules.cfg.entity.CfgSite;
import com.oplink.webems.modules.cfg.entity.CfgSlot;
import com.oplink.webems.modules.cfg.services.CfgNeService;
import com.oplink.webems.modules.cfg.services.CfgNetypeService;
import com.oplink.webems.modules.cfg.services.CfgSeverityService;
import com.oplink.webems.modules.cfg.services.CfgSiteService;
import com.oplink.webems.modules.cfg.services.CfgSlotService;
import com.oplink.webems.modules.sys.web.BaseController;

/**
 * History Alarm Controller
 * @author yizhang
 */
@Controller
@RequestMapping(value = "alarm/alarmhist")
public class AlarmHistoryController extends BaseController {

	@Autowired
	private AlarmHistoryService alarmHistoryService;
	
	@Autowired
	private AlarmPropService alarmPropService;
	
	@Autowired
	private CfgSiteService siteService;
	
	@Autowired
	private CfgNeService neService;
	
	@Autowired
	private CfgSeverityService severityService;
	
	@Autowired
	private CfgNetypeService netypeService;
	
	@Autowired
	private CfgSlotService slotService;
	
	@ModelAttribute
	public AlarmHistory get(@RequestParam(required = false) Long id) {
		if (id != null) {
			return alarmHistoryService.getAlarmHistory(id);
		} else {
			return new AlarmHistory();
		}
	}	
	
	//http://127.0.0.1:8080/webems/alarm/alarmhist/
	@RequestMapping(value = { "list", "" })
	public String list(AlarmHistory ahistory, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<CfgSite> slist = siteService.getAllSite();
		List<CfgNe> nelist = neService.getAllNe();
		List<AlarmSeverity> selist = severityService.getAllSeverity();
		List<AlarmType> atlist = alarmPropService.getAllAlarmType();
		List<CfgNeType> netypelist = netypeService.getAllNeType();
		List<CfgSlot> slotlist = slotService.getAllSlot();	
		//Long userId = getCurrentUserId();
		if (StringUtils.isEmpty(ahistory.getTimetype())){
			ahistory.setTimetype("adate");
		}
		if (StringUtils.isEmpty(ahistory.getBtimes())) {
			ahistory.setBtimes(DateUtils.getfirstDay());
		}
		if (StringUtils.isEmpty(ahistory.getEtimes())) {
			ahistory.setEtimes(DateUtils.getDate("yyyy-MM-dd HH:mm"));
		}
		Page<AlarmHistory> pages = alarmHistoryService.find(new Page<AlarmHistory>(request, response), ahistory);
		model.addAttribute("pages", pages);
		model.addAttribute("slist", slist);
		model.addAttribute("nelist", nelist);
		model.addAttribute("selist", selist);
		model.addAttribute("slotlist", slotlist);
		model.addAttribute("atlist", atlist);
		model.addAttribute("netypelist", netypelist);		
		model.addAttribute("ahist", ahistory);
		return "modules/alarm/alarm_hist";
	}
	
	@SuppressWarnings("rawtypes")
	@ResponseBody
	@RequestMapping(value = "stat")
	public String histAlarmStat(String btimes,String etimes,String timetype) {
		List<Map<String, Object>> list = alarmHistoryService.statAlarm(btimes, etimes, timetype);
		List<Map> statlist = alarmHistoryService.getStatMap(list);
        String json = JSONObject.toJSONString(statlist);
		return json;
	}	
}
